package cn.com.controller.operationController;


import cn.com.Enum.SessionAttr;
import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Seller;
import cn.com.db.model.User;
import cn.com.db.service.SellerService;
import cn.com.util.*;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Auth(UserAuth.SELLER)
@RequestMapping("/operation/seller")
@RestController
public class SellerOperationController {

    @Autowired
    SellerService sellerService;

    @Auth(UserAuth.ALL)
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public void register(HttpServletRequest request, HttpServletResponse response,
                         @RequestParam("authingId")String authingId,
                         @RequestParam("token")String token)
    {
        DataResult dataResult=new DataResult();
        if (sellerService.getSellerByAuthingId(authingId)!=null){

            dataResult.setCode(Code.SUCCESS);
            dataResult.setMessage("商家注册成功");
            util.comeBack(response, JSON.toJSONString(dataResult));
            return;
        }
        Seller seller=new Seller();
        seller.setAuthing_id(authingId);
        seller.setId(UidUtil.createUUID());
        seller.setToken(token);
        seller.setAble(false);
        seller.setVerify(false);
        seller.setCreated_at(TimeUtil.getNowTimestamp());
        seller.setUpdated_at(TimeUtil.getNowTimestamp());
        sellerService.createSeller(seller);
//        用户id存session
        SessionUtil.addAttr(request, SessionAttr.SELLERID,seller.getId());
        SessionUtil.addAttr(request,SessionAttr.SELLERAUTHINGID,seller.getAuthing_id());
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("注册成功");
        dataResult.setData(seller);
        response.setHeader("token",TokenUtil.createToken(seller.getId(),UserAuth.USER));
        util.comeBack(response,JSON.toJSONString(dataResult));
    }


    @Auth(UserAuth.ALL)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(HttpServletRequest request,HttpServletResponse response,
                      @RequestParam("authingId")String authingId)
    {
        DataResult dataResult=new DataResult();
        Seller seller=sellerService.getSellerByAuthingId(authingId);
        if (seller==null){
            dataResult.setCode(Code.UN_AUTHORIZED);
            dataResult.setMessage("登录失败,商家不存在");
            util.comeBack(response,JSON.toJSONString(dataResult));
            return;
        }
        if (!seller.getVerify()){
            dataResult.setCode(Code.FORBIDDEN);
            dataResult.setMessage("商家未通过验证,无法登录");
            util.comeBack(response,JSON.toJSONString(dataResult));
            return;
        }
        if (!seller.getAble()){
            dataResult.setCode(Code.FORBIDDEN);
            dataResult.setMessage("商家被禁用,无法登录");
            util.comeBack(response,JSON.toJSONString(dataResult));
            return;
        }
        SessionUtil.addAttr(request,SessionAttr.SELLERID,seller.getId());
        SessionUtil.addAttr(request,SessionAttr.SELLERAUTHINGID,seller.getAuthing_id());
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("登录成功");
        dataResult.setData(seller);
        response.setHeader("token",TokenUtil.createToken(seller.getId(),UserAuth.USER));
        util.comeBack(response,JSON.toJSONString(dataResult));
    }

}
